package com.link.base.tencent.corpwx.corppayment.controller;

import com.link.core.cllog.LCLogger;
import com.link.base.tencent.corpwx.corppayment.model.CorpRedPack;
import com.link.base.tencent.corpwx.corppayment.service.CorpPaymentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业支付
 *
 * @author liying
 * @date 2019/2/21
 */
@Controller
@RequestMapping("/link/corpwx/corppayment")
public class CorpPaymentController {

    @Resource
    private CorpPaymentService corpPaymentService;

    /**
     * 发放企业红包
     *
     * @author liying
     * @date 2019/02/21
     * @param record 传入参数
     */
    @PostMapping(value = "/sendCorpRedPack", name = "发放企业红包")
    @ResponseBody
    public Map<String, Object> sendCorpRedPack(@RequestBody CorpRedPack record,
                                               HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map> mapList;
        try {
            // 接口调用成功标识
            boolean flag = true;
            // 商户号
            String mchId = "1518876511";
            // 公众账号appId
            String wxAppId = "wwe71019f3bd03b677";
            // 发送红包的应用id
            int agentId = 1000015;
            record.setMchId(mchId);
            record.setWxAppId(wxAppId);
            record.setAgentId(agentId);
            // 接口返回报错信息
            String errorMsg = "";

            mapList = corpPaymentService.sendCorpRedPack(record);
            for (Map resultMap :
                    mapList) {
                // 判断接口返回码，返回前端报错信息
                if ("FAIL".equals(resultMap.get("return_code").toString())) {
                    flag = false;
                    errorMsg = resultMap.get("return_msg").toString();
                } else {
                    if ("FAIL".equals(resultMap.get("result_code").toString())) {
                        flag = false;
                        errorMsg = resultMap.get("err_code_des").toString();
                    }
                }
                if (!flag) {
                    result.put("message", errorMsg);
                    result.put("success", false);
                    break;
                }
            }
            if (flag) {
                result.put("message", mapList);
                result.put("success", true);
            }

        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }

        return result;
    }
}
